Imports CapaDeNegocios
Imports System.Data
Imports System

Partial Public Class Contactos
    Inherits System.Web.UI.Page
    Dim Proveedor As CapaDeNegocios.nProveedor = New CapaDeNegocios.nProveedor
    Dim Cliente As CapaDeNegocios.nCliente = New CapaDeNegocios.nCliente
    Dim Contacto As CapaDeNegocios.nContacto = New CapaDeNegocios.nContacto
    Dim Comprobaciones As CapaDeNegocios.nComprobaciones = New CapaDeNegocios.nComprobaciones


    Protected Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Inicio()
            CargarEmpresas()
        End If
    End Sub

    Private Sub Inicio()
        TxtApellido.Text = ""
        TxtDomicilio.Text = ""
        TxtFechaNacimiento.Text = ""
        TxtMail.Text = ""
        TxtTelefono.Text = ""
        Deshabilitar()

        CboTEmpresa.SelectedValue = -1
        CboEmpresas.SelectedValue = -1
        CboEmpresas.Enabled = False

        LnkBuscar.Visible = False
        LnkCancelar.Visible = False
        LnkEliminar.Visible = False
        LnkGuardar.Visible = False
        LblError.Visible = False

        HFIdContacto.Value = ""
        GridContactoProveedor.Visible = False
        GridContactoCliente.Visible = False
    End Sub

    Private Sub Habilitar()

        Select Case CboTEmpresa.SelectedValue
            Case Is = -1
                Deshabilitar()
            Case Is = 0 'cliente
                CargarGridContactoCliente()
            Case Is = 1 'proveedor
                CargarGridContactoProveedor()
        End Select


        Me.TxtApellido.Enabled = True
        Me.TxtDomicilio.Enabled = True
        Me.TxtFechaNacimiento.Enabled = True
        Me.TxtMail.Enabled = True
        Me.TxtTelefono.Enabled = True
    End Sub

    Private Sub Deshabilitar()
        Me.TxtApellido.Enabled = False
        Me.TxtDomicilio.Enabled = False
        Me.TxtFechaNacimiento.Enabled = False
        Me.TxtMail.Enabled = False
        Me.TxtTelefono.Enabled = False
        Me.TxtApellido.Text = ""
        Me.TxtDomicilio.Text = ""
        Me.TxtFechaNacimiento.Text = ""
        Me.TxtMail.Text = ""
        Me.TxtTelefono.Text = ""
    End Sub

    Private Sub CargarEmpresas()
        CboEmpresas.Items.Clear()
        CboEmpresas.Items.Add(New ListItem("Seleccione un Tipo de Empresa", -1))
        Select Case Me.CboTEmpresa.SelectedValue
            Case Is = -1
                CboTEmpresa.Focus()
                CboEmpresas.Enabled = False
            Case Is = 0 'Cliete
                CboEmpresas.DataSource = Cliente.Listar("", "", "", "", "", "", "", "", "", "", "", "")
                CboEmpresas.DataTextField = "RazonSocial"
                CboEmpresas.DataValueField = "CodigoCliente"
                CboEmpresas.DataBind()
                CargarGridContactoCliente()
            Case Is = 1 'Proveedor
                CboEmpresas.DataSource = Proveedor.Listar("", "", "", "", "", "", "", "")
                CboEmpresas.DataTextField = "RazonSocial"
                CboEmpresas.DataValueField = "CodigoProv"
                CboEmpresas.DataBind()
                CargarGridContactoProveedor()
        End Select
        CboEmpresas.SelectedIndex = -1
        CboEmpresas.Enabled = True
        LnkGuardar.Visible = True
    End Sub

    Sub CargarGridContactoProveedor()
        GridContactoProveedor.DataSource = Contacto.ListarGrid("", "", "", 2, "", "", "", "", "")
        GridContactoProveedor.DataBind()
        GridContactoProveedor.Visible = True
        GridContactoCliente.Visible = False
    End Sub

    Sub CargarGridContactoCliente()
        GridContactoCliente.DataSource = Contacto.ListarGrid("", "", "", 1, "", "", "", "", "")
        GridContactoCliente.DataBind()
        GridContactoProveedor.Visible = False
        GridContactoCliente.Visible = True
    End Sub

    Protected Sub CboTEmpresa_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboTEmpresa.SelectedIndexChanged
        If CboTEmpresa.SelectedValue = -1 Then
            Inicio()
        Else
            CboTEmpresa.BackColor = Drawing.Color.White
            LnkGuardar.Visible = True
            Habilitar()
            CargarEmpresas()
            LnkBuscar.Visible = True
            LnkBuscar.Enabled = True
            LnkCancelar.Visible = True
        End If
    End Sub

    Protected Sub CboEmpresas_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CboEmpresas.SelectedIndexChanged

        Select Case CboEmpresas.SelectedValue
            Case Is = -1
                Deshabilitar()
            Case Else
                Habilitar()
                LnkGuardar.Visible = True
        End Select
    End Sub


    Private Sub GridContactoProveedor_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridContactoProveedor.PageIndexChanging
        GridContactoProveedor.PageIndex = e.NewPageIndex
        CargarGridContactoProveedor()
    End Sub

    Private Sub GridContactoCliente_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridContactoCliente.PageIndexChanging

        GridContactoCliente.PageIndex = e.NewPageIndex
        CargarGridContactoCliente()
    End Sub

    Private Sub GridContactoCliente_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridContactoCliente.RowCommand
        If e.CommandName = "Select" Then
            TxtApellido.Text = e.CommandSource.Parent.Parent.FindControl("LblNombreApellido").text
            TxtDomicilio.Text = e.CommandSource.Parent.Parent.FindControl("LblDomicilio").text
            TxtTelefono.Text = e.CommandSource.Parent.Parent.FindControl("LblTelefono").text
            TxtMail.Text = e.CommandSource.Parent.Parent.FindControl("LblMail").text
            CboEmpresas.SelectedValue = e.CommandSource.Parent.Parent.FindControl("HFCodigoEmpresa").value
            Session("CodigoContacto") = e.CommandSource.Parent.FindControl("HFContactoCliente").value
            HFIdContacto.Value = Session("CodigoContacto")
            LnkEliminar.Visible = True
            LnkBuscar.Enabled = False
            LnkGuardar.Visible = True
            Habilitar()
        End If
    End Sub

    Private Sub GridContactoProveedor_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridContactoProveedor.RowCommand
        If e.CommandName = "Select" Then
            TxtApellido.Text = e.CommandSource.Parent.Parent.FindControl("LblNombreApellido").text
            TxtDomicilio.Text = e.CommandSource.Parent.Parent.FindControl("LblDomicilio").text
            TxtTelefono.Text = e.CommandSource.Parent.Parent.FindControl("LblTelefono").text
            TxtMail.Text = e.CommandSource.Parent.Parent.FindControl("LblMail").text
            CboEmpresas.SelectedValue = e.CommandSource.Parent.Parent.FindControl("HFCodigoEmpresa").value
            Session("CodigoContacto") = e.CommandSource.Parent.FindControl("HFContactoProveedor").value
            HFIdContacto.Value = Session("CodigoContacto")
            LnkEliminar.Visible = True
            LnkBuscar.Enabled = False
            LnkGuardar.Visible = True
            Habilitar()
        End If
    End Sub

    Protected Sub LnkGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkGuardar.Click
        Dim oEmpresa As String
        Dim oTEmpresa As String

        If CboEmpresas.SelectedValue = -1 Then
            LblError.Visible = True
            LblError.Text = "Seleccione una Empresa, para su Contacto"
            Exit Sub
        Else
            oEmpresa = CboEmpresas.SelectedValue
        End If

        If Comprobaciones.FN_EsFecha(TxtFechaNacimiento.Text) = False Then
            LblError.Visible = True
            LblError.Text = "Fecha No Valida"
            Exit Sub
        End If

        If Comprobaciones.Fn_Mail(TxtMail.Text) = False Then
            LblError.Visible = True
            LblError.Text = "Verifique el Formato del Mail"
            Exit Sub
        End If


        If HFIdContacto.Value.ToString = "" Then
            Select Case CboTEmpresa.SelectedValue
                Case Is = -1
                    Me.Response.Write("<script languaje='javascript'>window.alert('Seleccione un Tipo De Empresa Valido')</script>")
                    Exit Sub
                Case Is = 0 'cliete
                    oTEmpresa = 1
                    Contacto.Insertar(oEmpresa, TxtApellido.Text, oTEmpresa, TxtFechaNacimiento.Text, TxtMail.Text, TxtTelefono.Text, "")
                    GridContactoCliente.DataSource = Contacto.ListarGrid("", _
                                                oEmpresa, _
                                                TxtApellido.Text, _
                                                oTEmpresa, _
                                                TxtDomicilio.Text, _
                                                TxtFechaNacimiento.Text, _
                                                TxtMail.Text, _
                                                TxtTelefono.Text, _
                                                "")
                    GridContactoCliente.DataBind()
                    LnkGuardar.Visible = False
                    LnkEliminar.Visible = False
                Case Is = 1 'Proveedor
                    oTEmpresa = 2
                    Contacto.Insertar(oEmpresa, TxtApellido.Text, oTEmpresa, TxtFechaNacimiento.Text, TxtMail.Text, TxtTelefono.Text, "")
                    GridContactoCliente.DataSource = Contacto.ListarGrid("", _
                                                oEmpresa, _
                                                TxtApellido.Text, _
                                                oTEmpresa, _
                                                TxtDomicilio.Text, _
                                                TxtFechaNacimiento.Text, _
                                                TxtMail.Text, _
                                                TxtTelefono.Text, _
                                                "")
                    GridContactoCliente.DataBind()
                    LnkGuardar.Visible = False
                    LnkEliminar.Visible = False
            End Select


        Else


            Select Case CboTEmpresa.SelectedValue
                Case Is = -1
                    Me.Response.Write("<script languaje='javascript'>window.alert('Seleccione un Tipo De Empresa Valido')</script>")
                    Exit Sub
                Case Is = 0 'cliete
                    oTEmpresa = 1
                    Contacto.Modificar(HFIdContacto.Value, oEmpresa, oTEmpresa, TxtApellido.Text, TxtDomicilio.Text, TxtFechaNacimiento.Text, TxtMail.Text, TxtTelefono.Text, "")

                    GridContactoCliente.DataSource = Contacto.ListarGrid(HFIdContacto.Value, "", "", 1, "", "", "", "", "")
                    GridContactoCliente.DataBind()

                    HFIdContacto.Value = ""
                    LnkGuardar.Visible = False
                    LnkEliminar.Visible = False
                Case Is = 1 'Proveedor
                    oTEmpresa = 2
                    Contacto.Modificar(HFIdContacto.Value, oEmpresa, oTEmpresa, TxtApellido.Text, TxtDomicilio.Text, TxtFechaNacimiento.Text, TxtMail.Text, TxtTelefono.Text, "")
                    GridContactoProveedor.DataSource = Contacto.ListarGrid(HFIdContacto.Value, "", "", 2, "", "", "", "", "")
                    GridContactoProveedor.DataBind()
                    HFIdContacto.Value = ""
                    LnkGuardar.Visible = False
                    LnkEliminar.Visible = False
            End Select
        End If

        LblError.Visible = False
        LblError.Text = ""
    End Sub

    Protected Sub LnkBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkBuscar.Click
        Dim oEmpresa As String
        Dim oTEmpresa As String

        If CboEmpresas.SelectedValue = -1 Then
            oEmpresa = ""
        Else
            oEmpresa = CboEmpresas.SelectedValue

        End If

        Select Case CboTEmpresa.SelectedValue
            Case Is = -1
                Me.Response.Write("<script languaje='javascript'>window.alert('Seleccione un Tipo De Empresa Valido')</script>")
                Exit Sub
            Case Is = 0 'cliete
                oTEmpresa = 1
                GridContactoCliente.DataSource = Contacto.ListarGrid("", _
                                                                oEmpresa, _
                                                                TxtApellido.Text, _
                                                                oTEmpresa, _
                                                                TxtDomicilio.Text, _
                                                                TxtFechaNacimiento.Text, _
                                                                TxtMail.Text, _
                                                                TxtTelefono.Text, _
                                                                "")
                GridContactoCliente.DataBind()
                GridContactoProveedor.Visible = False
                GridContactoCliente.Visible = True

            Case Is = 1 'Proveedor
                oTEmpresa = 2
                GridContactoProveedor.DataSource = Contacto.ListarGrid("", _
                                                                oEmpresa, _
                                                                TxtApellido.Text, _
                                                                oTEmpresa, _
                                                                TxtDomicilio.Text, _
                                                                TxtFechaNacimiento.Text, _
                                                                TxtMail.Text, _
                                                                TxtTelefono.Text, _
                                                                "")
                GridContactoProveedor.DataBind()
                GridContactoProveedor.Visible = True
                GridContactoCliente.Visible = False
        End Select

    End Sub

    Protected Sub LnkCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LnkCancelar.Click
        Inicio()
        Deshabilitar()
    End Sub
End Class